package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Document;
import com.util.DBHelp;
import com.util.RowMapper;

public class DocumentDao {
	
	private DBHelp<Document> help=new DBHelp<Document>();

	public List<Document> getByPid(String id) {
		String sql="SELECT `id`,`title`,`content`,`createtime`,`employeeid`,`projectid` FROM `t_document` WHERE `projectid`=? ORDER BY `createtime` DESC";
		return help.executeQueryForList(sql, new Mapper(), id);
	}
	
	private class Mapper implements RowMapper<Document>{

		public Document mapperRow(ResultSet rs) throws SQLException {
			Document document=new Document();
			document.setContent(rs.getString("content"));
			document.setCreatetime(rs.getString("createtime"));
			document.setEmployeeid(rs.getString("employeeid"));
			document.setId(rs.getString("id"));
			document.setProjectid(rs.getString("projectid"));
			document.setTitle(rs.getString("title"));
			
			return document;
		}
		
	}

	public Document getByid(String id) {
		String sql="SELECT `id`,`title`,`content`,`createtime`,`employeeid`,`projectid` FROM `t_document` WHERE `id`=?";
		return help.executeQueryForObject(sql, new Mapper(), id);
	}

	public void update(Document document) {
		String sql="UPDATE `t_document` SET `title`=?,`content`=? WHERE id=?";
		help.executeSQL(sql, document.getTitle(),document.getContent(),document.getId());
	}

	public void del(String id) {
		String sql="DELETE FROM `t_document` WHERE id=?";
		help.executeSQL(sql, id);
	}

	public void add(Document document) {
		String sql="INSERT INTO `t_document`(`id`,`title`,`content`,`createtime`,`employeeid`,`projectid`) VALUES(?,?,?,?,?,?)";
		help.executeSQL(sql, document.getId(),document.getTitle(),document.getContent(),document.getCreatetime(),document.getEmployeeid(),document.getProjectid());
	}

}
